package eu.pago.chat.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class FriendsFrame extends JFrame {

	private static final Color AVAILABLE_GREEN = new Color(182, 215, 168);
	private static final Color OFFLINE_GREY = new Color(220, 220, 235);
	private static final Color BUSY_RED = new Color(234, 153, 153);
	private final List<InfoFriend> users;
	private static final long serialVersionUID = 1L;
	private static final Color FADE_YELLOW = new Color(255, 242, 204);
	private final List<JButton> friendButtons;

	public FriendsFrame(List<InfoFriend> users) {
		JPanel panel = new JPanel();
		this.add(panel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Friends");
		this.setVisible(true);
		this.users = users;
		this.setSize(250, 30 * users.size() + 100);
		friendButtons = new ArrayList<JButton>();
		this.setJMenuBar(createMenu());
		this.add(createMessagePanel(), BorderLayout.NORTH);
		this.add(createButtonsPanel(), BorderLayout.CENTER);
	}

	private JMenuBar createMenu() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Menu");
		JMenuItem addFriendMenu = new JMenuItem("Add Friend");

		JMenuItem refreshMenu = new JMenuItem("Refresh");
		menu.add(addFriendMenu);
		menu.add(refreshMenu);

		JMenu statusMenu = new JMenu("Status");
		JMenuItem available = new JMenuItem("Available");
		JMenuItem busy = new JMenuItem("Busy");
		JMenuItem invisible = new JMenuItem("Invisible");

		menu.add(addFriendMenu);
		menu.add(refreshMenu);

		statusMenu.add(available);
		statusMenu.add(busy);
		statusMenu.add(invisible);

		menuBar.add(menu);
		menuBar.add(statusMenu);

		return menuBar;
	}

	private JPanel createMessagePanel() {
		JPanel messagePanel = new JPanel();
		messagePanel.setBackground(FADE_YELLOW);
		JLabel messageLabel = new JLabel("Click on friend ID to connect");
		messageLabel.setHorizontalTextPosition(JLabel.CENTER);
		messagePanel.add(messageLabel);
		return messagePanel;
	}

	private JPanel createButtonsPanel() {
		JPanel friendsPanel = new JPanel();
		friendsPanel.setBackground(FADE_YELLOW);
		int gridH = users.size() > 0 ? users.size() : 1;
		friendsPanel.setLayout(new GridLayout(gridH, 0, 5, 5));
		for (InfoFriend user : users) {
			JButton friendButton = new JButton(user.getNume());
			friendButton.setPreferredSize(new Dimension(200, 40));
			switch (user.getStatus()) {
			case AVAILABLE:
				friendButton.setBackground(AVAILABLE_GREEN);
				break;
			case BUSY:
				friendButton.setBackground(BUSY_RED);
				break;
			case OFFLINE:
				friendButton.setBackground(OFFLINE_GREY);
				break;
			}
			friendButtons.add(friendButton);
			friendsPanel.add(friendButton);
		}
		return friendsPanel;
	}
}
